package com.eugene.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eugene.element.FnCalendar;
import com.eugene.element.ScenarioDef;
import com.eugene.element.ScenarioRst;
import com.eugene.entity.Scenario;
import com.eugene.enums.EMaturity;
import com.eugene.util.HibernateUtil;

public class ScenarioDAO {
	private final static Logger logger = LoggerFactory.getLogger(ScenarioDAO.class);	
	
	public static List<ScenarioDef> getScenarioDef(String sceDefId, FnCalendar baseDate){
		List<ScenarioDef> sce = new ArrayList<ScenarioDef>();
		Session s = HibernateUtil.currentSession();
		
		s.enableFilter("dateFilter").setParameter("asOfDate", baseDate);
		s.enableFilter("lessThan").setParameter("asOfDate", baseDate.minusTerm(EMaturity.M02, true));
		
		Query qr =s.createQuery(" from ScenarioDef a where a.sceDefId =:param" );
		qr.setParameter("param", sceDefId);
		
		sce=qr.list();
		return sce;
	}
	public static List<ScenarioRst> getScenarioRstNew(String sceSetId, FnCalendar baseDate){
		List<ScenarioRst> sce = new ArrayList<ScenarioRst>();
		
		Session s = HibernateUtil.currentSession();
		s.enableFilter("dateFilter").setParameter("asOfDate", baseDate);
//		s.enableFilter("lessThan").setParameter("asOfDate", baseDate.minusTerm(EMaturity.M02, true));
		
		Query qr =s.createQuery(" from ScenarioRstNew a where a.sceSetId =:param" );
		qr.setParameter("param", sceSetId);
		
		sce=qr.list();
		return sce;
	}

	
	
	
	
	
	



	public static List<Scenario> getScenarioAll(FnCalendar baseDate){
		List<Scenario> sce = new ArrayList<Scenario>();
		sce.addAll(ScenarioDAO.getScenarioUd(baseDate));
		sce.addAll(ScenarioDAO.getScenarioHs(baseDate));
		sce.addAll(ScenarioDAO.getScenarioMc(baseDate));
		return sce;
	}
	public static List<Scenario> getScenarioAll(String sceSetId,FnCalendar baseDate){
		List<Scenario> sce = new ArrayList<Scenario>();
		sce.addAll(ScenarioDAO.getScenarioUd(sceSetId,baseDate));
		sce.addAll(ScenarioDAO.getScenarioHs(sceSetId,baseDate));
		sce.addAll(ScenarioDAO.getScenarioMc(sceSetId,baseDate));
		return sce;
	}
//	public static List<Scenario> getScenarioAll(String sceDefId, String mvId, FnCalendar baseDate){
//		List<Scenario> sce = new ArrayList<Scenario>();
//		sce.add(ScenarioDAO.getScenarioUd(sceDefId,mvId,baseDate));
//		sce.add(ScenarioDAO.getScenarioHs(sceDefId,mvId,baseDate));
//		sce.add(ScenarioDAO.getScenarioMc(sceDefId,mvId,baseDate));
//		return sce;
//	}
	
	public static List<Scenario> getScenarioUd(FnCalendar baseDate){
		List<Scenario> sce = new ArrayList<Scenario>();
		Session s = HibernateUtil.currentSession();
		s.enableFilter("dateFilter").setParameter("asOfDate", baseDate);
		
		Query qr =s.createQuery(" from ScenarioUd" );
		
		sce=qr.list();
//		HibernateUtil.closeSession();
		return sce;
	}
	public static List<Scenario> getScenarioUd(String sceSetId, FnCalendar baseDate){
		List<Scenario> sce = new ArrayList<Scenario>();
		Session s = HibernateUtil.currentSession();
		s.enableFilter("dateFilter").setParameter("asOfDate", baseDate);
		
		
		Query qr =s.createQuery(" from ScenarioUd a " +
								" where a.sceSetId =:param"
		);
		qr.setParameter("param", sceSetId);
		sce=qr.list();
//		HibernateUtil.closeSession();
		return sce;
	}
	public static Scenario getScenarioUd(String sceDefId, String mvId, FnCalendar baseDate){
		Scenario sce ;
		Session s = HibernateUtil.currentSession();
		s.enableFilter("dateFilter").setParameter("asOfDate", baseDate);
		
		Query qr =s.createQuery(" from ScenarioUd a " +
								" where a.sceDefId =:param" +
								" and a.mvId = :param2" );
		qr.setParameter("param", sceDefId);
		qr.setParameter("param2", mvId);
		
		sce=(Scenario)qr.uniqueResult();
//		HibernateUtil.closeSession();
		return sce;
	}

	public static List<Scenario> getScenarioHs(FnCalendar baseDate){
		List<Scenario> sce = new ArrayList<Scenario>();
		Session s = HibernateUtil.currentSession();
		s.enableFilter("dateFilter").setParameter("asOfDate", baseDate);
		
		Query qr =s.createQuery(" from ScenarioHs" );
		
		sce=qr.list();
//		HibernateUtil.closeSession();
		return sce;
	}
	public static List<Scenario> getScenarioHs(String sceSetId, FnCalendar baseDate){
		List<Scenario> sce = new ArrayList<Scenario>();
		Session s = HibernateUtil.currentSession();
		s.enableFilter("dateFilter").setParameter("asOfDate", baseDate);
		
		Query qr =s.createQuery(" from ScenarioHs a " +
				" where a.sceSetId =:param"
		);
		qr.setParameter("param", sceSetId);
		
		
		sce=qr.list();
//		HibernateUtil.closeSession();
		return sce;
	}
	public static Scenario getScenarioHs(String sceDefId, String mvId, FnCalendar baseDate){
		Scenario sce ;
		Session s = HibernateUtil.currentSession();
		s.enableFilter("dateFilter").setParameter("asOfDate", baseDate);
		
		Query qr =s.createQuery(" from ScenarioHs a " +
								" where a.sceDefId =:param" +
								" and a.mvId = :param2" );
		qr.setParameter("param", sceDefId);
		qr.setParameter("param2", mvId);
		
		sce=(Scenario)qr.uniqueResult();
//		HibernateUtil.closeSession();
		return sce;
	}
	
	public static List<Scenario> getScenarioMc(FnCalendar baseDate){
		List<Scenario> sce = new ArrayList<Scenario>();
		Session s = HibernateUtil.currentSession();
		s.enableFilter("dateFilter").setParameter("asOfDate", baseDate);
		
		Query qr =s.createQuery(" from ScenarioMc" );
		
		sce=qr.list();
//		HibernateUtil.closeSession();
		return sce;
	}
	public static List<Scenario> getScenarioMc(String sceSetId, FnCalendar baseDate){
		List<Scenario> sce = new ArrayList<Scenario>();
		Session s = HibernateUtil.currentSession();
		s.enableFilter("dateFilter").setParameter("asOfDate", baseDate);
		
		Query qr =s.createQuery(" from ScenarioMc a " +
								" where a.sceSetId =:param"
		);
		qr.setParameter("param", sceSetId);
		
		
		sce=qr.list();
//		HibernateUtil.closeSession();
		return sce;
	}
	public static Scenario getScenarioMc(String sceDefId, String mvId, FnCalendar baseDate){
		Scenario sce ;
		Session s = HibernateUtil.currentSession();
		s.enableFilter("dateFilter").setParameter("asOfDate", baseDate);
		
		Query qr =s.createQuery(" from ScenarioMc a " +
								" where a.sceDefId =:param" +
								" and a.mvId = :param2" );
		qr.setParameter("param", sceDefId);
		qr.setParameter("param2", mvId);
		
		sce=(Scenario)qr.uniqueResult();
//		HibernateUtil.closeSession();
		return sce;
	}

//**********************get ScenarioRst****************
	
	public static List<Scenario> getScenarioRst(String sceId, FnCalendar baseDate){
		List<Scenario> rst = new ArrayList<Scenario>();
		Session s = HibernateUtil.currentSession();
		
		s.enableFilter("dateFilter").setParameter("asOfDate", baseDate);
		Query qr =s.createQuery(" from ScenarioRst a " +
								" where a.sceId =:param" 
								);
		qr.setParameter("param", sceId);
		rst= qr.list();
		
		return rst;
		
		
	}
	public static List<Scenario> getScenarioRst(String sceId,String mvId, FnCalendar baseDate){
		List<Scenario> rst = new ArrayList<Scenario>();
		Session s = HibernateUtil.currentSession();
		
		s.enableFilter("dateFilter").setParameter("asOfDate", baseDate);
		Query qr =s.createQuery(" from ScenarioRst a " +
								" where a.sceId =:param" +
								" and a.mvId = :param2 "
								);
		qr.setParameter("param", sceId);
		qr.setParameter("param2", mvId);
		rst= qr.list();
		
		return rst;
		
	}
	public static List<Scenario> getScenarioRst(String sceId,String mvId,EMaturity maturity, FnCalendar baseDate){
		List<Scenario> rst = new ArrayList<Scenario>();
		Session s = HibernateUtil.currentSession();
		
		s.enableFilter("dateFilter").setParameter("asOfDate", baseDate);
		Query qr =s.createQuery(" from ScenarioRst a " +
								" where a.sceId =:param" +
								" and a.mvId = :param2 " +
								" and a.maturity =:param3"
								);
		qr.setParameter("param", sceId);
		qr.setParameter("param2", mvId);
		qr.setParameter("param3", maturity);
		rst= qr.list();
		
		return rst;
	}
	
	public static List<Scenario> getScenarioRstBySceSet(String sceSetId, FnCalendar baseDate){
		List<Scenario> rst = new ArrayList<Scenario>();
		Session s = HibernateUtil.currentSession();
		
		s.enableFilter("dateFilter").setParameter("asOfDate", baseDate);
		Query qr =s.createQuery(" from ScenarioRst a " +
								" where a.sceSetId =:param" 
								
								);
		qr.setParameter("param", sceSetId);
		rst= qr.list();
		
		return rst;
	}
	
	
//**************************Write to the DB**********************
	
	public static void setScenarioRst(Scenario sce, FnCalendar baseDate){
		Session s = HibernateUtil.currentSession();
		Transaction tx = s.beginTransaction();
		s.saveOrUpdate("ScenarioRst",sce);
		tx.commit();
	}
	
//	public static void setScenarioRst(List<Scenario> sce, FnCalendar baseDate){
//		Session s = HibernateUtil.currentSession();
//		Transaction tx = s.beginTransaction();
//		for(Scenario aa:sce){
//			s.saveOrUpdate("ScenarioRst",aa);
//		}
//		tx.commit();
//		HibernateUtil.closeSession();
//	}
	
	public static void setScenarioRst(List<ScenarioRst> sce, FnCalendar baseDate){
		Session s = HibernateUtil.currentSession();
		Transaction tx = s.beginTransaction();
		for(ScenarioRst aa:sce){
			s.saveOrUpdate("ScenarioRstNew",aa);
		}
		tx.commit();
	}
	

//********************For DAO**************	
	public static List<String> getScenarioSetIds(){
		List<String> sceSetIds = new ArrayList<String>();
		Session s = HibernateUtil.currentSession();
		
		Query qr =s.createQuery("select distinct a.sceSetId from ScenarioRst a " );
		sceSetIds=qr.list();
		return sceSetIds;
	}
//****************Temporary*******************************	
	public static List<Scenario> getScenario(String sceId){
		List<Scenario> sce = new ArrayList<Scenario>();
		
		Session s = HibernateUtil.currentSession();
		Query qr =s.createQuery("from ScenarioRst a " +
								"where a.sceId =:param " );
		qr.setParameter("param", sceId);
		sce=qr.list();
//		HibernateUtil.closeSession();
		return sce;
	}
	public static List<Scenario> getScenarioSet(String sceSetId){
		List<Scenario> sce = new ArrayList<Scenario>();
		
		Session s = HibernateUtil.currentSession();
		Query qr =s.createQuery("from ScenarioRst a " +
								"where a.sceSetId =:param " );
		qr.setParameter("param", sceSetId);
		sce=qr.list();
//		HibernateUtil.closeSession();
		return sce;
	}
	
	
	
	
	
//	public static List<Scenario> getScenarios(){
//		List<Scenario> sce = new ArrayList<Scenario>();
//		Session s = HibernateUtil.currentSession();
//		
//		Query qr =s.createQuery("select distinct a from ScenarioView a" );
//		
//		sce=qr.list();
////		HibernateUtil.closeSession();
//		return sce;
//	}
	
////	public static List<Scenario> getScenarios(String sceSetId){
//	public static List<Scenario> getScenarios(String sceId){
//		List<Scenario> sce = new ArrayList<Scenario>();
//		
//		Session s = HibernateUtil.currentSession();
//		
////		Query qr =s.createQuery("select distinct a from ScenarioView a where a.sceSetId =:param " );
////		qr.setParameter("param", sceSetId);
//		Query qr =s.createQuery("select distinct a from ScenarioView a where a.sceId =:param " );
//		qr.setParameter("param", sceId);
//		sce=qr.list();
//		HibernateUtil.closeSession();
//		return sce;
//
//	}
	
	

	
//	public static List<Scenario> getScenarioSet(){
//		List<Scenario> sce = new ArrayList<Scenario>();
//		Session s = HibernateUtil.currentSession();
//		
//		Query qr =s.createQuery("select distinct a from ScenarioSetView a " );
//		
//		sce=qr.list();
//			
//		HibernateUtil.closeSession();
//		return sce;
//	}
//	public static List<Scenario> getScenarioSet(String sceSetId){
//		List<Scenario> sce = new ArrayList<Scenario>();
//		Session s = HibernateUtil.currentSession();
//		
//		Query qr =s.createQuery("select distinct a from ScenarioSetView a where a.sceSetId = :param" );
//		qr.setParameter("param", sceSetId);
//		sce=qr.list();
//			
//		HibernateUtil.closeSession();
//		return sce;
//	}
}
